{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "royal-canon",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import warnings\n",
    "import sys\n",
    "import datetime\n",
    "\n",
    "sys.path.append(\"..\")\n",
    "\n",
    "from imp import reload\n",
    "\n",
    "import common\n",
    "reload(common)\n",
    "reload(common.analysis_helper)\n",
    "\n",
    "from common.config_helper import config_helper\n",
    "from common.analysis_helper import analysis_helper\n",
    "\n",
    "#import common.date_helper\n",
    "#reload(common.date_helper)\n",
    "\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "common-missile",
   "metadata": {},
   "outputs": [],
   "source": [
    "config = config_helper()\n",
    "start_date = \"20200101\"\n",
    "end_date = \"20201231\"\n",
    "pro = ts.pro_api(config.tushare_token)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "joined-victory",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>change</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601899.SH</td>\n",
       "      <td>20201231</td>\n",
       "      <td>9.32</td>\n",
       "      <td>9.43</td>\n",
       "      <td>9.16</td>\n",
       "      <td>9.29</td>\n",
       "      <td>9.27</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.2157</td>\n",
       "      <td>2438899.64</td>\n",
       "      <td>2267849.820</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>601899.SH</td>\n",
       "      <td>20201230</td>\n",
       "      <td>9.09</td>\n",
       "      <td>9.36</td>\n",
       "      <td>8.99</td>\n",
       "      <td>9.27</td>\n",
       "      <td>9.10</td>\n",
       "      <td>0.17</td>\n",
       "      <td>1.8681</td>\n",
       "      <td>2451022.32</td>\n",
       "      <td>2258500.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>601899.SH</td>\n",
       "      <td>20201229</td>\n",
       "      <td>9.51</td>\n",
       "      <td>9.55</td>\n",
       "      <td>9.10</td>\n",
       "      <td>9.10</td>\n",
       "      <td>9.44</td>\n",
       "      <td>-0.34</td>\n",
       "      <td>-3.6017</td>\n",
       "      <td>2440644.36</td>\n",
       "      <td>2254838.142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>601899.SH</td>\n",
       "      <td>20201228</td>\n",
       "      <td>9.37</td>\n",
       "      <td>9.65</td>\n",
       "      <td>9.35</td>\n",
       "      <td>9.44</td>\n",
       "      <td>9.27</td>\n",
       "      <td>0.17</td>\n",
       "      <td>1.8339</td>\n",
       "      <td>3210290.10</td>\n",
       "      <td>3055390.719</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>601899.SH</td>\n",
       "      <td>20201225</td>\n",
       "      <td>8.93</td>\n",
       "      <td>9.30</td>\n",
       "      <td>8.71</td>\n",
       "      <td>9.27</td>\n",
       "      <td>8.91</td>\n",
       "      <td>0.36</td>\n",
       "      <td>4.0404</td>\n",
       "      <td>2733569.14</td>\n",
       "      <td>2487214.501</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code trade_date  open  high   low  close  pre_close  change  pct_chg  \\\n",
       "0  601899.SH   20201231  9.32  9.43  9.16   9.29       9.27    0.02   0.2157   \n",
       "1  601899.SH   20201230  9.09  9.36  8.99   9.27       9.10    0.17   1.8681   \n",
       "2  601899.SH   20201229  9.51  9.55  9.10   9.10       9.44   -0.34  -3.6017   \n",
       "3  601899.SH   20201228  9.37  9.65  9.35   9.44       9.27    0.17   1.8339   \n",
       "4  601899.SH   20201225  8.93  9.30  8.71   9.27       8.91    0.36   4.0404   \n",
       "\n",
       "          vol       amount  \n",
       "0  2438899.64  2267849.820  \n",
       "1  2451022.32  2258500.597  \n",
       "2  2440644.36  2254838.142  \n",
       "3  3210290.10  3055390.719  \n",
       "4  2733569.14  2487214.501  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_zj = pro.daily(ts_code=\"601899.SH\", start_date = start_date, end_date = end_date)\n",
    "df_zj.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "unknown-boost",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>b_zero</th>\n",
       "      <th>zero</th>\n",
       "      <th>s_zero</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [b_zero, zero, s_zero]\n",
       "Index: []"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "reload(common.analysis_helper)\n",
    "ana = analysis_helper(df_zj)\n",
    "df_open = ana.Open2Preclose()\n",
    "df_high =ana.High2Preclose()\n",
    "df_open.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "anticipated-surfing",
   "metadata": {},
   "outputs": [],
   "source": [
    "def Open2Preclose(df):\n",
    "        df['o2pre'] = pd.DataFrame(df.open - df.pre_close)\n",
    "        destdf = pd.DataFrame()\n",
    "        destdf['b_zero'] = df.o2pre[df.o2pre > 0].count()\n",
    "        destdf['zero'] = df.o2pre[df.o2pre == 0].count()\n",
    "        destdf['s_zero'] = df.o2pre[df.o2pre < 0].count()\n",
    "        \n",
    "        return destdf\n",
    "\n",
    "def High2Preclose(self):\n",
    "    df['h2pre'] = pd.DataFrame(df.high - df.pre_close)\n",
    "    destdf = pd.DataFrame()\n",
    "    destdf['b_zero'] = df.h2pre[df.h2pre > 0].count()\n",
    "    destdf['zero'] = df.h2pre[df.h2pre == 0].count()\n",
    "    destdf['s_zero'] = df.h2pre[df.h2pre < 0].count()\n",
    "\n",
    "    return destdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "falling-modification",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'DataFrame' object has no attribute 'high'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-63-bb5ddd933db0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mHigh2Preclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdf_zj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhead\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-62-cc078d2b4ecf>\u001b[0m in \u001b[0;36mHigh2Preclose\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mHigh2Preclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m     \u001b[0mdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'h2pre'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhigh\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpre_close\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     10\u001b[0m     \u001b[0mdestdf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m     \u001b[0mdestdf\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'b_zero'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mh2pre\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mdf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mh2pre\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcount\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.pyenv/versions/3.7.0/lib/python3.7/site-packages/pandas/core/generic.py\u001b[0m in \u001b[0;36m__getattr__\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m   5139\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_info_axis\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_can_hold_identifiers_and_holds_name\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   5140\u001b[0m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 5141\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mobject\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__getattribute__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   5142\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   5143\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m__setattr__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalue\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'DataFrame' object has no attribute 'high'"
     ]
    }
   ],
   "source": [
    "df = High2Preclose(df_zj)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "informational-breakdown",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>change</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "      <th>open2preclose</th>\n",
       "      <th>o2pre</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>601899.SH</td>\n",
       "      <td>20201231</td>\n",
       "      <td>9.32</td>\n",
       "      <td>9.43</td>\n",
       "      <td>9.16</td>\n",
       "      <td>9.29</td>\n",
       "      <td>9.27</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.2157</td>\n",
       "      <td>2438899.64</td>\n",
       "      <td>2267849.820</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>601899.SH</td>\n",
       "      <td>20201230</td>\n",
       "      <td>9.09</td>\n",
       "      <td>9.36</td>\n",
       "      <td>8.99</td>\n",
       "      <td>9.27</td>\n",
       "      <td>9.10</td>\n",
       "      <td>0.17</td>\n",
       "      <td>1.8681</td>\n",
       "      <td>2451022.32</td>\n",
       "      <td>2258500.597</td>\n",
       "      <td>-0.01</td>\n",
       "      <td>-0.01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>601899.SH</td>\n",
       "      <td>20201229</td>\n",
       "      <td>9.51</td>\n",
       "      <td>9.55</td>\n",
       "      <td>9.10</td>\n",
       "      <td>9.10</td>\n",
       "      <td>9.44</td>\n",
       "      <td>-0.34</td>\n",
       "      <td>-3.6017</td>\n",
       "      <td>2440644.36</td>\n",
       "      <td>2254838.142</td>\n",
       "      <td>0.07</td>\n",
       "      <td>0.07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>601899.SH</td>\n",
       "      <td>20201228</td>\n",
       "      <td>9.37</td>\n",
       "      <td>9.65</td>\n",
       "      <td>9.35</td>\n",
       "      <td>9.44</td>\n",
       "      <td>9.27</td>\n",
       "      <td>0.17</td>\n",
       "      <td>1.8339</td>\n",
       "      <td>3210290.10</td>\n",
       "      <td>3055390.719</td>\n",
       "      <td>0.10</td>\n",
       "      <td>0.10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>601899.SH</td>\n",
       "      <td>20201225</td>\n",
       "      <td>8.93</td>\n",
       "      <td>9.30</td>\n",
       "      <td>8.71</td>\n",
       "      <td>9.27</td>\n",
       "      <td>8.91</td>\n",
       "      <td>0.36</td>\n",
       "      <td>4.0404</td>\n",
       "      <td>2733569.14</td>\n",
       "      <td>2487214.501</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.02</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code trade_date  open  high   low  close  pre_close  change  pct_chg  \\\n",
       "0  601899.SH   20201231  9.32  9.43  9.16   9.29       9.27    0.02   0.2157   \n",
       "1  601899.SH   20201230  9.09  9.36  8.99   9.27       9.10    0.17   1.8681   \n",
       "2  601899.SH   20201229  9.51  9.55  9.10   9.10       9.44   -0.34  -3.6017   \n",
       "3  601899.SH   20201228  9.37  9.65  9.35   9.44       9.27    0.17   1.8339   \n",
       "4  601899.SH   20201225  8.93  9.30  8.71   9.27       8.91    0.36   4.0404   \n",
       "\n",
       "          vol       amount  open2preclose  o2pre  \n",
       "0  2438899.64  2267849.820           0.05   0.05  \n",
       "1  2451022.32  2258500.597          -0.01  -0.01  \n",
       "2  2440644.36  2254838.142           0.07   0.07  \n",
       "3  3210290.10  3055390.719           0.10   0.10  \n",
       "4  2733569.14  2487214.501           0.02   0.02  "
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_zj.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "lasting-lending",
   "metadata": {},
   "outputs": [],
   "source": [
    "df['o2pre'] = pd.DataFrame(df_zj.open - df_zj.pre_close)\n",
    "#df.o2pre[df.o2pre > 0].count()[0]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
